package com.push.web.base;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.utils.FilePersistentBase;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @类名 CrawlerSupport
 * @描述 TODO
 * @作者 wang yanxi
 * @版本 1.0
 * @日期 2021-8-9 15:47
 */
public abstract class CrawlerSupport extends FilePersistentBase {
    public static final String HEADER_CONTENT_TYPE_KEY = "content-type";
    public static final String HEADER_USER_AGENT_KEY = "User-Agent";
    public static final String USER_AGENT_FIREFOX_OS = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0";
    public static final String USER_AGENT_CHROME_OS = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36";
    public static final String USER_AGENT_IPHONE_OS = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) " +
            "AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1";
    public static final String USER_AGENT_ANDROID_OS = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Mobile Safari/537.36";

    public static final String NEED_SAVE_KEY = "NEED_SAVE";
    public static final String NEED_SAVE_YES = "YES";
    public static final String NEED_SAVE_NO = "NO";

    protected boolean isNeedSave(ResultItems resultItems) {
        if (NEED_SAVE_YES.equals(resultItems.get(NEED_SAVE_KEY)))
            return true;
        return false;
    }

    protected void setNeedSaveWithYes(Page page) {
        page.putField(NEED_SAVE_KEY, NEED_SAVE_YES);
    }

    protected String findRegex(String content, String regex) {
        Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);
        Matcher ma = pa.matcher(content);
        while (ma.find()) {
            return ma.group();
        }
        return "";
    }


    protected String clearNewLine(String str) {
        return str.replaceAll("\r\n", "")
                .replaceAll("\n", "");
    }

    protected String noTitleGoToArticleGetTitle(String article) {
        int titleLength = 20;
        String handleArticle = limitMaxCharacter(article, titleLength);

        String resultTitle = article.substring(0,
                firstPunctuationIndex(handleArticle, 0));

        return handleArticle.substring(0,
                firstPunctuationIndex(resultTitle, 0));
    }

    protected String noAbstractGoToArticleGetAbstract(String article) {
        int maxCharacter = 150;
        String limitArticle = limitMaxCharacter(article, maxCharacter);
        int lastIndex = limitArticle.length();

        int lastPunctuationIndex = lastPunctuationIndex(limitArticle, lastIndex);
        return limitArticle.substring(0, lastPunctuationIndex) + "。";
    }

    private String limitMaxCharacter(String str, int maxSize) {
        int strLength = str.length();
        String result;
        if (strLength > maxSize) {
            result = str.substring(0, maxSize + 1);
        } else {
            result = str;
        }
        return result;
    }

    protected int firstPunctuationIndex(String str, int startIndex) {
        int firstStopIndex = str.indexOf("。", startIndex);
        if (firstStopIndex == -1) {
            int firstCommaIndex = str.indexOf("，");
            if (firstCommaIndex == -1) {
                return str.length();
            } else {
                return firstCommaIndex;
            }
        } else {
            return firstStopIndex;
        }
    }

    protected int lastPunctuationIndex(String str, int lastIndex) {
        int lastStopIndex = str.lastIndexOf("。", lastIndex);
        if (lastStopIndex == -1) {
            int lastCommaIndex = str.lastIndexOf("，");
            if (lastCommaIndex == -1) {
                return str.length();
            } else {
                return lastCommaIndex;
            }
        } else {
            return lastStopIndex;
        }
    }

    protected Map<String, String> analyticalUrlParams(String strUrl) {
        Map<String, String> mapRequest = new HashMap<>();

        String[] arrSplit;
        String strUrlParam = TruncateUrlPage(strUrl);
        if (strUrlParam == null) {
            return mapRequest;
        }

        arrSplit = strUrlParam.split("[&]");
        for (String strSplit : arrSplit) {
            String[] arrSplitEqual;
            arrSplitEqual = strSplit.split("[=]");

            if (arrSplitEqual.length > 1) {
                mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
            } else {
                if (arrSplitEqual[0] != "") {
                    mapRequest.put(arrSplitEqual[0], "");
                }
            }
        }
        return mapRequest;
    }

    private static String TruncateUrlPage(String strUrl) {
        String strAllParam = null;
        String[] arrSplit = null;

        strUrl = strUrl.trim();

        arrSplit = strUrl.split("[?]");
        if (strUrl.length() > 1) {
            if (arrSplit.length > 1) {
                if (arrSplit[1] != null) {
                    strAllParam = arrSplit[1];
                }
            } else {
                strAllParam = strUrl;
            }
        }
        return strAllParam;
    }

    protected boolean isHtml(String string) {
        if (string.trim().indexOf("<!") != -1) {
            return true;
        }
        return false;
    }

    private static final String REGEX_CLASS_STYLE =
            "((>)(&nbsp;)+)" +
                    "|(onload=\\\".+\\\")" +
                    "|(id=\"[^\"]+\")" +
                    "|(class=\"[^\"]+\")" +
                    "|(style=\"[^\"]+\")" +
                    "|(<script.*?>.*?</script>)" +
                    "|(点击图片看专题)" +
                    "|(返回腾讯网首页>>)" +
                    "|(返回腾讯网首页&gt;&gt;)" +
                    "|(展开阅读全文)" +
                    "|(查看更多)";
}
